import { addDialog, closeDialog } from "@/components/ReDialog";
import { debounce, deviceDetection } from "@pureadmin/utils";
import protocolContent from '../components/protocolContent.vue'
import { storageLocal } from "@pureadmin/utils";
import { Click as gocaptchaClick, Slide as gocaptchaSlide } from 'go-captcha-vue'
import { getCaptcha } from "@/api/user";
import {  customPromiseDebounce } from "@/utils/functions";
const openProtocolContent = (protocolName, protocolContentInfo) => {
  // addDialog({
  //     title: protocolName,
  //     hideFooter: true,
  //     width: "800px",
  //     draggable: true,
  //     fullscreen: deviceDetection(),
  //     fullscreenIcon: true,
  //     closeOnClickModal: false,
  //     props: {
  //         formInline: {
  //             protocolName: protocolName,
  //             protocolContentInfo: protocolContentInfo
  //         }
  //     },
  //     contentRenderer: () => h(protocolContent),
  // });
  storageLocal().setItem("protocolName", protocolName);
  storageLocal().setItem("protocolContentInfo", protocolContentInfo);
  window.open(
    window.location.protocol + "//" + window.location.host + "/#/protocolContent",
    "_blank"
  );
}
const robotVerification = ({ fun, param = {}, successFun = (res) => { }, errorFun = () => { }, finallyFun = () => { } }) => {
  const customPromiseDebounceFun=customPromiseDebounce(fun,500,true)
  getCaptcha().then(res => {
    const data = ref(res.result)
    const key = ref(0)
    const getcode=()=>getCaptcha().then(res => {
      data.value = res.result
      key.value++
    })
    const debounceGetcode=debounce(getcode,500,true)
    addDialog({
      title: `请完成安全验证`,
      width: "auto",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: false,
      closeOnClickModal: false,
      hideFooter: true,
      contentRenderer: ({ options, index }) => h(data.value.captchaType == 1 ? gocaptchaClick : gocaptchaSlide, {
        config: {},
        data: data.value,
        key: key.value,
        events: {
          refresh: () => {
            debounceGetcode()
          },
          confirm: (point) => {
            let { captchaId, captchaType } = data.value
            let closePopup = () => closeDialog(options, index)
            // { point, captchaId, captchaType, closePopup: () => closeDialog(options, index) }
            customPromiseDebounceFun({ ...param, point, captchaId, captchaType }).then(res => {
              successFun(res)
              closePopup()
            }).catch(() => {
              debounceGetcode()
              errorFun()
            }).finally(() => {
              finallyFun()
            })
          }
        }
      }),
    })
  });

}
export { openProtocolContent, robotVerification }
